<template>
    <div>
        <el-table :data="tableData" style="width: 100%" row-key="id" border lazy :expand-row-keys="rowId">
            <el-table-column prop="name" label="名称" width="370">
            </el-table-column>
            <el-table-column prop="code" label="权限值" width="370">
            </el-table-column>
            <el-table-column prop="toCode" width="370" label="跳转权限值">
            </el-table-column>
            <el-table-column prop="operation" width="370" label="操作">
                <!-- slot-scope="{row}" -->
                <template #default="{ row }">
                    <el-tooltip class="item" effect="dark" content="添加菜单" placement="top-start">
                        <el-button type="primary" icon="el-icon-plus" :disabled="row.level === 4" size="mini"
                            title="添加菜单" @click="toAddMenu(row)">

                        </el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="修改菜单" placement="top-start">
                        <el-button type="primary" size="mini" icon="el-icon-edit"
                            :title="row.level === 4 ? '修改功能' : '修改菜单'" :disabled="row.level === 1" @click="editMenu(row)">
                        </el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                        <el-button size="mini" type="danger" title="删除" icon="el-icon-delete"
                            :disabled="row.level === 1" @click="delMenu(row)">
                        </el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <!-- 添加菜单 -->
        <el-dialog :title="ruleForm.id ? `修改菜单` : '添加菜单'" :visible.sync="dialogVisible" :before-close="closeHandler">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="功能权限值" prop="code">
                    <el-input v-model="ruleForm.code"></el-input>
                </el-form-item>
                <el-form-item label="跳转路由权限值" prop="toCode" v-if="ruleForm.level === 4">
                    <el-input v-model="ruleForm.toCode" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrEditMenu">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'



const menuRules = {
    name: [{ required: true, message: '名称必须输入' ,trigger:"blur"}],
    code: [{ required: true, message: '权限值必须输入',trigger:"blur" }],
}

const btnRules = {
    name: [{ required: true, message: '名称必须输入',trigger:"blur" }],
    code: [{ required: true, trigger: 'blur', message: '功能权限值必须输入'}]
}

export default {
    data() {
        return {
            dialogVisible: false,// 是否显示菜单权限的Dialog
            Modifymenu: false,
            tableData: [],// 菜单列表数据
            rowId: [],
            ruleForm: {
                name: '',
                level: 0,
                code: '',
                toCode: ''
            },
            permissionList: []
        }
    },
    computed: {
        rules() {
            return this.ruleForm.level === 4 ? btnRules : menuRules
            // name: { required: true, message: '名称必须输入' },
            // jurisdiction: { required: true, message: '权限值必须输入' },\
        }
    },
    mounted() {
        this.getpermission()
    },
    methods: {
        async getpermission() {
            try {
                let result = await this.$API.permission.getPermissionList()
                this.tableData = result.data.children
                this.rowId = [this.tableData[0].id]
                // console.log(result)
            } catch (error) {
                console.error(error)
            }
        },
        //新增
        toAddMenu(row) {
            this.dialogVisible = true
            this.ruleForm.pid = row.id
            this.ruleForm.level = row.level + 1
            this.ruleForm.type = this.ruleForm.level === 4 ? 2 : 1

        },
        //修改
        editMenu(row) {
            this.dialogVisible = true
            this.ruleForm = cloneDeep(row)
            this.ruleForm.level = row.level + 1;

        },
        closeHandler() {
            this.dialogVisible = false;
            this.ruleForm = {}
        },
        // 确定删除
        delMenu(row) {
            this.$confirm(`此操作将永久删除${row.name}, 是否继续?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let result = await this.$API.permission.removePermission(row.id)
                this.$message.success(result.message || '删除成功!')
                this.getpermission()
            }).catch((error) => {
                if (error === 'cancel') {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                }
            });
        },

        //添加或更新菜单
        addOrEditMenu() {
            this.$refs.ruleForm.validate(async (success) => {
                if (success) {
                    try {
                        this.dialogVisible = false
                        if (!this.ruleForm.id) {
                            await this.$API.permission.addPermission(this.ruleForm)
                        } else {
                            await this.$API.permission.updatePermission(this.ruleForm)
                            this.ruleForm={}
                        }
                        this.$message({
                            message: this.ruleForm.id ? '修改成功' : '添加成功',
                            type: 'success'
                        })
                        this.getpermission()
                        this.resetData()
                    } catch (error) {
                        console.error(error)
                    }
                }
            })
        },
        //重置数据
        resetData() {
            this.dialogVisible = false
            this.permission = {
                level: 0,
                name: '',
                code: '',
                toCode: ''
            }
        }

    },
}

</script>

<style  scoped>

</style>